﻿Imports System.IO
Public Class QuestEditor
#Region "Declarations and Dimensions"
    Inherits System.Windows.Forms.Form
    Implements IMessageFilter
    Private WM_KEYDOWN As Integer = &H100
    Private WM_KEYUP As Integer = &H101
    Private CampaignDir As String
    Private SessionDir As String
    Public SessionData As String
    Private Const VertScrollBar As Int32 = &H115
    Private Const VertScrollBar_Btm As Int32 = 7
    Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
#End Region
#Region "Functions"
    Public Function GetFileContents(ByVal FullPath As String, _
         Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try
            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            Return strContents
            objReader.Close()
            objReader.Dispose()
        Catch Ex As Exception
            ErrInfo = Ex.Message
            MsgBox(ErrInfo, MsgBoxStyle.Critical, "ERROR!")
            Return ""
        End Try
    End Function
    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        Dim bAns As Boolean = False
        Dim trynum As Short = 1
        Dim objReader As StreamWriter
        While bAns = False
            Try
                trynum += 1
                objReader = New StreamWriter(FullPath)
                objReader.Write(strData)
                objReader.Close()
                bAns = True
            Catch Ex As Exception
                ErrInfo = Ex.Message
                If trynum = 100 Then
                    MsgBox(ErrInfo, MsgBoxStyle.Critical, "ERROR!")
                    bAns = True
                End If
            End Try
        End While
        Return bAns
    End Function
    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
        Dim keyCode As Keys = CType(CInt(m.WParam), Keys) And Keys.KeyCode
        If m.Msg = WM_KEYDOWN And keyCode = Keys.Escape Then
            Me.Close()
            Application.RemoveMessageFilter(Me) : Return True
        End If
        Return False
    End Function 'PreFilterMessage
    Public Sub SND(ByVal Txt As String)
        OUTPUT.Select(Len(OUTPUT.Text), 0)
        Txt = "/B " + Txt + "/B"
        Dim Switches As Integer = -1
        Dim TempStr As String
        Dim TempStr2 As String
        Dim TempInt As Integer
        Dim TempInt2 As Integer = 0
        For TempInt = 1 To Len(Txt) Step 1
            TempStr = Mid(Txt, TempInt, 1)
            If TempStr = "/" Then
                If Mid(Txt, TempInt, 2) = "/B" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFF)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/b" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H66)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/C" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFFFF)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/c" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H6666)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/M" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFF00FF)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/m" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H660066)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/y" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H666600)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/Y" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFFFF00)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/g" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H6600)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/G" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFF00)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/w" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H666666)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/W" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HAAAAAA)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/z" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H333333)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/Z" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFFFFFF)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/R" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&HFF0000)
                    TempInt2 = TempInt
                    Switches += 1
                ElseIf Mid(Txt, TempInt, 2) = "/r" Then
                    If TempInt2 <> 0 Then
                        TempStr2 = Mid(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Txt, "/b", ""), "/B", ""), "/g", ""), "/G", ""), "/m", ""), "/M", ""), "/w", ""), "/W", ""), "/y", ""), "/Y", ""), "/z", ""), "/Z", ""), "/c", ""), "/C", ""), "/r", ""), "/R", ""), TempInt2 - Switches * 2, TempInt - TempInt2 - 2)
                        OUTPUT.SelectedText = TempStr2
                    End If
                    OUTPUT.SelectionColor = Color.FromArgb(&H660000)
                    TempInt2 = TempInt
                    Switches += 1
                End If
            End If
        Next
        OUTPUT.SelectedText = Chr(13)
        SendMessage(OUTPUT.Handle, VertScrollBar, VertScrollBar_Btm, 0)
    End Sub
#End Region
#Region "Open & Close"
    Private Sub QuestEditorLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(Me)
        Dim FolderFound As Boolean = True, Num As Integer = 1
        CampaignList.Items.Clear()
        While FolderFound = True
            If My.Computer.FileSystem.DirectoryExists(Debugger.DirQuests + "\Campaign" + LTrim(Str(Num))) = True Then
                CampaignList.Items.Add("Campaign " + LTrim(Str(Num)))
                Num += 1
            Else
                FolderFound = False
            End If
        End While
        CampaignList.SelectedIndex = 0
        CampaignDir = Debugger.DirQuests + "\Campaign1"
        FolderFound = True : Num = 1
        SessionList.Items.Clear()
        While FolderFound = True
            If My.Computer.FileSystem.FileExists(CampaignDir + "\Session" + LTrim(Str(Num)) + ".TG") = True Then
                SessionList.Items.Add("Session " + LTrim(Str(Num)))
                Num += 1
            Else
                FolderFound = False
            End If
        End While
        SessionDir = CampaignDir + "\Session1.TG"
        SessionList.SelectedIndex = 0
    End Sub
    Private Sub CloseMe(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
#End Region
#Region "Reload"
    Private Sub ReloadSessions(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CampaignList.SelectedIndexChanged
        Dim FolderFound As Boolean = True, Num As Integer = 1
        CampaignDir = Debugger.DirQuests + "\Campaign" + LTrim(Str(CampaignList.SelectedIndex + 1))
        SessionList.Items.Clear()
        While FolderFound = True
            If My.Computer.FileSystem.FileExists(CampaignDir + "\Session" + LTrim(Str(Num)) + ".TG") = True Then
                SessionList.Items.Add("Session " + LTrim(Str(Num)))
                Num += 1
            Else
                FolderFound = False
            End If
        End While
        SessionList.SelectedIndex = 0
        SessionDir = CampaignDir + "\Session" + LTrim(Str(SessionList.SelectedIndex + 1)) + ".TG"
    End Sub
    Private Sub ReloadParts(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SessionList.SelectedIndexChanged
        Dim PartFound As Boolean = True, Num As Integer = 1
        SessionData = GetFileContents(CampaignDir + "/Session" + LTrim(Str(SessionList.SelectedIndex + 1)) + ".TG")
        PartList.Items.Clear()
        If InStr(SessionData, "CAMPA#&(") Then
            Dim tmp0 As Integer
            For tmp0 = InStr(SessionData, "CAMPA#&(") + 8 To Len(SessionData) Step 1
                If Mid(SessionData, tmp0, 1) = ")" Then
                    CampaignName.Text = Mid(SessionData, InStr(SessionData, "CAMPA#&(") + 8, tmp0 - 8 - InStr(SessionData, "CAMPA#&("))
                    Exit For
                End If
            Next
        End If
        If InStr(SessionData, "SESSI#&(") Then
            Dim tmp0 As Integer
            For tmp0 = InStr(SessionData, "SESSI#&(") + 8 To Len(SessionData) Step 1
                If Mid(SessionData, tmp0, 1) = ")" Then
                    SessionName.Text = Mid(SessionData, InStr(SessionData, "SESSI#&(") + 8, tmp0 - 8 - InStr(SessionData, "SESSI#&("))
                    Exit For
                End If
            Next
        End If
        If InStr(SessionData, "INITM#&(") Then
            Dim tmp0 As Integer
            For tmp0 = InStr(SessionData, "INITM#&(") + 8 To Len(SessionData) Step 1
                If Mid(SessionData, tmp0, 1) = ")" Then
                    TextBox1.Text = Mid(SessionData, InStr(SessionData, "INITM#&(") + 8, tmp0 - 8 - InStr(SessionData, "INITM#&("))
                    Exit For
                End If
            Next
        End If
        While PartFound = True
            If InStr(SessionData, "SCRIP#" + LTrim(Str(Num)) + "(") Then
                PartList.Items.Add("Part " + LTrim(Str(Num)))
                Num += 1
            Else
                PartFound = False
            End If
        End While
        PartList.SelectedIndex = 0
        SessionDir = CampaignDir + "\Session" + LTrim(Str(SessionList.SelectedIndex + 1)) + ".TG"
    End Sub
#End Region
    Private Sub LoadScript(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PartList.SelectedIndexChanged
        Dim tmp10 As Integer
        Dim tmpstr As String = "", SCRIPTnum As String = "", LocString As String = Replace(PartList.Items(PartList.SelectedIndex), "Part ", "") + "."
        If PartList.Items(PartList.SelectedIndex) <> "" Then
            For tmp10 = 1 To Len(LocString) Step 1
                If Mid(LocString, tmp10, 1) = "." Then
                    tmpstr = Mid(LocString, 1, tmp10 - 1)
                    SCRIPTnum = tmpstr
                    Exit For
                End If
            Next
        Else
            SND("/RERROR: No SCRIT number inputed")
        End If
        ScriptEditor.QuestOpened = True
        ScriptEditor.ScriptData = SessionData
        ScriptEditor.LoadScript(SCRIPTnum)
        Dim tempvar As Integer = InStr(SessionData, "SCRIP#" + SCRIPTnum + "(")
        Dim tempvar2 As Integer, tempstr As String
        Dim tmp1 As Integer = 0, count As Single = 0
        If tempvar > 0 Then
            For tempvar2 = InStr(SessionData, "SCRIP#" + SCRIPTnum + "(") To Len(SessionData) Step 1
                If Mid(SessionData, tempvar2, 1) = ")" Then
                    tempstr = Replace(Mid(SessionData, tempvar + 7 + Len(LTrim(RTrim(SCRIPTnum))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(SCRIPTnum))))), " ", "")
                    Exit For
                End If
            Next
            SND("/yLoading script objective...")
            tempvar = InStr(SessionData, "OBJEC#" + SCRIPTnum + "(")
            For tempvar2 = InStr(SessionData, "OBJEC#" + SCRIPTnum + "(") To Len(SessionData) Step 1
                If Mid(SessionData, tempvar2, 1) = ")" Then
                    tempstr = Mid(SessionData, tempvar + 7 + Len(LTrim(RTrim(SCRIPTnum))), +tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(SCRIPTnum)))))
                    If tempstr = "1" Then
                        RadioButton1.Checked = True
                    ElseIf tempstr = "2" Then
                        RadioButton2.Checked = True
                    ElseIf tempstr = "3" Then
                        RadioButton3.Checked = True
                    ElseIf tempstr = "4" Then
                        RadioButton4.Checked = True
                    ElseIf tempstr = "5" Then
                        RadioButton5.Checked = True
                    End If
                    Exit For
                End If
            Next
        End If
    End Sub
    Private Sub EditScript(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim tmp10 As Integer
        Dim tmpstr As String = "", SCRIPTnum As String = "", LocString As String = Replace(PartList.Items(PartList.SelectedIndex), "Part ", "") + "."
        If PartList.Items(PartList.SelectedIndex) <> "" Then
            For tmp10 = 1 To Len(LocString) Step 1
                If Mid(LocString, tmp10, 1) = "." Then
                    tmpstr = Mid(LocString, 1, tmp10 - 1)
                    SCRIPTnum = tmpstr
                    Exit For
                End If
            Next
        Else
            SND("/RERROR: No SCRIT number inputed")
        End If
        ScriptEditor.QuestOpened = True
        ScriptEditor.ScriptData = SessionData
        ScriptEditor.LoadScript(SCRIPTnum)
        ScriptEditor.Show()
    End Sub
#Region "Save"
    Private Sub SaveQuests(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SND("/YSaving...")
        Dim tmp0 As Integer
        For tmp0 = InStr(SessionData, "CAMPA#&(") + 8 To Len(SessionData) Step 1
            If Mid(SessionData, tmp0, 1) = ")" Then
                SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "CAMPA#&("), tmp0 - InStr(SessionData, "CAMPA#&(")), "CAMPA#&(" + CampaignName.Text)
                Exit For
            End If
        Next
        For tmp0 = InStr(SessionData, "SESSI#&(") + 8 To Len(SessionData) Step 1
            If Mid(SessionData, tmp0, 1) = ")" Then
                SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "SESSI#&("), tmp0 - InStr(SessionData, "SESSI#&(")), "SESSI#&(" + SessionName.Text)
                Exit For
            End If
        Next
        For tmp0 = InStr(SessionData, "INITM#&(") + 8 To Len(SessionData) Step 1
            If Mid(SessionData, tmp0, 1) = ")" Then
                SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "INITM#&("), tmp0 - InStr(SessionData, "INITM#&(")), "INITM#&(" + TextBox1.Text)
                Exit For
            End If
        Next
        Dim tmp10 As Integer
        Dim tmpstr As String = "", SCRIPTnum As String = "", LocString As String = Replace(PartList.Items(PartList.SelectedIndex), "Part ", "") + "."
        If PartList.Items(PartList.SelectedIndex) <> "" Then
            For tmp10 = 1 To Len(LocString) Step 1
                If Mid(LocString, tmp10, 1) = "." Then
                    tmpstr = Mid(LocString, 1, tmp10 - 1)
                    SCRIPTnum = LTrim(RTrim(tmpstr))
                    Exit For
                End If
            Next
        Else
            SND("/RERROR: No SCRIT number inputed")
        End If
        For tmp0 = InStr(SessionData, "OBJEC#" + SCRIPTnum + "(") + 8 To Len(SessionData) Step 1
            If Mid(SessionData, tmp0, 1) = ")" Then
                If RadioButton1.Checked = True Then
                    SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "OBJEC#" + SCRIPTnum + "("), tmp0 - InStr(SessionData, "OBJEC#" + SCRIPTnum + "(")), "OBJEC#" + SCRIPTnum + "(1")
                ElseIf RadioButton2.Checked = True Then
                    SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "OBJEC#" + SCRIPTnum + "("), tmp0 - InStr(SessionData, "OBJEC#" + SCRIPTnum + "(")), "OBJEC#" + SCRIPTnum + "(2")
                ElseIf RadioButton3.Checked = True Then
                    SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "OBJEC#" + SCRIPTnum + "("), tmp0 - InStr(SessionData, "OBJEC#" + SCRIPTnum + "(")), "OBJEC#" + SCRIPTnum + "(3")
                ElseIf RadioButton4.Checked = True Then
                    SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "OBJEC#" + SCRIPTnum + "("), tmp0 - InStr(SessionData, "OBJEC#" + SCRIPTnum + "(")), "OBJEC#" + SCRIPTnum + "(4")
                ElseIf RadioButton5.Checked = True Then
                    SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "OBJEC#" + SCRIPTnum + "("), tmp0 - InStr(SessionData, "OBJEC#" + SCRIPTnum + "(")), "OBJEC#" + SCRIPTnum + "(5")
                End If
                Exit For
            End If
        Next
        SaveTextToFile(SessionData, SessionDir)
        SND("/YSaved Script!")
    End Sub
    Private Sub SaveQuestsAndExit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SaveQuests(0, EventArgs.Empty)
        Me.Close()
    End Sub
#End Region
#Region "Extend Campaign/Session/Parts"
    Private Sub CreatePart(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If InStr(SessionData, "TOTAL#" + LTrim(Str(PartList.Items.Count)) + "(") Then
            Dim tmp0 As Integer
            For tmp0 = InStr(SessionData, "TOTAL#" + LTrim(Str(PartList.Items.Count)) + "(") + 7 + Len(LTrim(Str(PartList.Items.Count))) To Len(SessionData) Step 1
                If Mid(SessionData, tmp0, 1) = ")" Then
                    SessionData = Replace(SessionData, Mid(SessionData, InStr(SessionData, "TOTAL#" + LTrim(Str(PartList.Items.Count)) + "("), tmp0 - InStr(SessionData, "TOTAL#" + LTrim(Str(PartList.Items.Count)) + "(")), "TOTAL#" + LTrim(Str(PartList.Items.Count + 1)) + "(" + LTrim(Str(PartList.Items.Count + 1)))
                    Exit For
                End If
            Next
        End If
        SessionData += Chr(13) + Chr(13) + "SCRIP#" + LTrim(Str(PartList.Items.Count + 1)) + "(" + LTrim(Str(PartList.Items.Count + 1)) + ")"
        SessionData += Chr(13) + "OBJEC#" + LTrim(Str(PartList.Items.Count + 1)) + "(1)"
        SessionData += Chr(13) + "ONCOM#" + LTrim(Str(PartList.Items.Count + 1)) + "(0)"
        SessionData += Chr(13) + "INTRO#" + LTrim(Str(PartList.Items.Count + 1)) + "(Blank Introduction.)"
        SessionData += Chr(13) + "OPTIO#" + LTrim(Str(PartList.Items.Count + 1)) + "(1.[Blank Option])"
        SessionData += Chr(13) + "REFRE#" + LTrim(Str(PartList.Items.Count + 1)) + "(Blank Refresh Response.)"
        SessionData += Chr(13) + "SUBO1#" + LTrim(Str(PartList.Items.Count + 1)) + "(Blank Response 1.)"
        PartList.Items.Add("Part " + LTrim(Str(PartList.Items.Count + 1)))
        PartList.SelectedIndex = PartList.Items.Count - 1
    End Sub
    Private Sub CreateCampaign(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim DefaultSession As String
        If My.Computer.FileSystem.DirectoryExists(Debugger.DirQuests + "\Campaign" + LTrim(Str(CampaignList.Items.Count + 1))) = False Then
            CampaignDir = Debugger.DirQuests + "\Campaign" + LTrim(Str(CampaignList.Items.Count + 1))
            My.Computer.FileSystem.CreateDirectory(CampaignDir)
            Dim fs As FileStream
            fs = File.Create(CampaignDir + "\Session1.TG")
            SessionDir = CampaignDir + "\Session1.TG"
            fs.Close()
            CampaignList.Items.Add("Campaign " + LTrim(Str(CampaignList.Items.Count + 1)))
            DefaultSession = "//QUESTS TERMINAL GROUPING"
            DefaultSession += Chr(13) + "//---------BEG HEADER"
            DefaultSession += Chr(13) + "// NOTE: REPLACE _ WITH & IN ACTUAL HEADER AND ! WITH #."
            DefaultSession += Chr(13) + "//CAMPA!_($) - $ = Campaign name"
            DefaultSession += Chr(13) + "//SESSI!_($) - $ = Session name"
            DefaultSession += Chr(13) + "//INITM#_(#1.#2,#1.#2,..) - #1 = Mobile Number"
            DefaultSession += Chr(13) + "//                          #2 = SCRIPT NUMBER TO REPLACE MOBILE DEFAULT SCRIPT"
            DefaultSession += Chr(13) + "//TOTAL!%(%) - % = Total quest script number"
            DefaultSession += Chr(13) + "//---------END HEADER"
            DefaultSession += Chr(13) + "//"
            DefaultSession += Chr(13) + "//SCRIP!?(?) - ? = PART OF SESSION NUMBER, QUEST SCRIPT"
            DefaultSession += Chr(13) + "//OBJEC!?(%) - % = SCRIPT RESPONSE REQUIREMENT FOR CAMPAIGN_SESSION_PART TO SUCCEED"
            DefaultSession += Chr(13) + "//ONCOM!?(#1.#2,#1.#2,..) - #1 = MOBILE NUMBER"
            DefaultSession += Chr(13) + "//                          #2 = SCRIPT NUMBER TO REPLACE MOBILES DEFAULT SCRIPT"
            DefaultSession += Chr(13) + "//REWAR!?(#) - # = reward experience for completing this quest, if 0, then ignores"
            DefaultSession += Chr(13) + "//INTRO!?($) - $ = INTRODUCTION TEXT FOR SCRIPT"
            DefaultSession += Chr(13) + "//OPTIO!?(1.[$],2.[$],3.[$],4.[$],5.[$]) - $ = Option 1-5 strings, each optional"
            DefaultSession += Chr(13) + "//REFRE!?($) - $ = REFRESH TEXT FOR SCRIPT"
            DefaultSession += Chr(13) + "//SUBO1!?($) - $ = RESPONSE 1 STRING"
            DefaultSession += Chr(13) + "//SUBO2!?($) - $ = RESPONSE 2 STRING"
            DefaultSession += Chr(13) + "//SUBO3!?($) - $ = RESPONSE 3 STRING"
            DefaultSession += Chr(13) + "//SUBO4!?($) - $ = RESPONSE 4 STRING"
            DefaultSession += Chr(13) + "//SUBO5!?($) - $ = RESPONSE 5 STRING"
            DefaultSession += Chr(13) + "//ENDTM!?($) - $ = Not decompiled, used for clay notes"
            DefaultSession += Chr(13) + "//"
            DefaultSession += Chr(13) + "//NOTE THAT EACH TARGETQUEST SHOULD BE CONNECTED TO ITS NEXT PART TO PRESERVER ORDER"
            DefaultSession += Chr(13) + "//WITHIN THE QUESTS. TARQU WILL BE AUTOMATED BY THE QUEST EDITOR, BUT MANUALLY EDITING"
            DefaultSession += Chr(13) + "//IT HERE IS DANGEROUS. "
            DefaultSession += Chr(13) + "////////////////////////////////////////////////////////////////////"
            DefaultSession += Chr(13) + "CAMPA#&(Blank Campaign)"
            DefaultSession += Chr(13) + "SESSI#&(Blank Session)"
            DefaultSession += Chr(13) + "INITM#&(0)"
            DefaultSession += Chr(13)
            DefaultSession += Chr(13) + "TOTAL#1(1)"
            DefaultSession += Chr(13)
            DefaultSession += Chr(13) + "SCRIP#1(1)"
            DefaultSession += Chr(13) + "OBJEC#1(1)"
            DefaultSession += Chr(13) + "ONCOM#1(0)"
            DefaultSession += Chr(13) + "INTRO#1(Blank Introduction text.)"
            DefaultSession += Chr(13) + "OPTIO#1(1.[Blank Option])"
            DefaultSession += Chr(13) + "REFRE#1(Blank Refresh Response text.)"
            DefaultSession += Chr(13) + "SUBO1#1(Blank Response 1.)"
            SaveTextToFile(DefaultSession, SessionDir)
            CampaignList.SelectedIndex = CampaignList.Items.Count - 1
        Else
            SND("/RError: Campaign was hidden, direct fault. Reload quest editor.")
        End If
    End Sub
    Private Sub CreateSession(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim DefaultSession As String
        If My.Computer.FileSystem.FileExists(CampaignDir + "Session" + LTrim(Str(SessionList.Items.Count + 1)) + ".TG") = False Then
            Dim fs As FileStream
            fs = File.Create(CampaignDir + "\Session" + LTrim(Str(SessionList.Items.Count + 1)) + ".TG")
            SessionDir = CampaignDir + "\Session" + LTrim(Str(SessionList.Items.Count + 1)) + ".TG"
            fs.Close()
            DefaultSession = "//QUESTS TERMINAL GROUPING"
            DefaultSession += Chr(13) + "//---------BEG HEADER"
            DefaultSession += Chr(13) + "// NOTE: REPLACE _ WITH & IN ACTUAL HEADER AND ! WITH #."
            DefaultSession += Chr(13) + "//CAMPA!_($) - $ = Campaign name"
            DefaultSession += Chr(13) + "//SESSI!_($) - $ = Session name"
            DefaultSession += Chr(13) + "//INITM!_(#1.#2,#1.#2,..) - #1 = Mobile Number"
            DefaultSession += Chr(13) + "//                          #2 = SCRIPT NUMBER TO REPLACE MOBILE DEFAULT SCRIPT"
            DefaultSession += Chr(13) + "//TOTAL!%(%) - % = Total quest script number"
            DefaultSession += Chr(13) + "//---------END HEADER"
            DefaultSession += Chr(13) + "//"
            DefaultSession += Chr(13) + "//SCRIP!?(?) - ? = PART OF SESSION NUMBER, QUEST SCRIPT"
            DefaultSession += Chr(13) + "//OBJEC!?(%) - % = SCRIPT RESPONSE REQUIREMENT FOR CAMPAIGN_SESSION_PART TO SUCCEED"
            DefaultSession += Chr(13) + "//ONCOM!?(#1.#2,#1.#2,..) - #1 = MOBILE NUMBER"
            DefaultSession += Chr(13) + "//                          #2 = SCRIPT NUMBER TO REPLACE MOBILES DEFAULT SCRIPT"
            DefaultSession += Chr(13) + "//REWAR!?(#) - # = reward experience for completing this quest, if 0, then ignores"
            DefaultSession += Chr(13) + "//INTRO!?($) - $ = INTRODUCTION TEXT FOR SCRIPT"
            DefaultSession += Chr(13) + "//OPTIO!?(1.[$],2.[$],3.[$],4.[$],5.[$]) - $ = Option 1-5 strings, each optional"
            DefaultSession += Chr(13) + "//REFRE!?($) - $ = REFRESH TEXT FOR SCRIPT"
            DefaultSession += Chr(13) + "//SUBO1!?($) - $ = RESPONSE 1 STRING"
            DefaultSession += Chr(13) + "//SUBO2!?($) - $ = RESPONSE 2 STRING"
            DefaultSession += Chr(13) + "//SUBO3!?($) - $ = RESPONSE 3 STRING"
            DefaultSession += Chr(13) + "//SUBO4!?($) - $ = RESPONSE 4 STRING"
            DefaultSession += Chr(13) + "//SUBO5!?($) - $ = RESPONSE 5 STRING"
            DefaultSession += Chr(13) + "//ENDTM!?($) - $ = Not decompiled, used for clay notes"
            DefaultSession += Chr(13) + "//"
            DefaultSession += Chr(13) + "//NOTE THAT EACH TARGETQUEST SHOULD BE CONNECTED TO ITS NEXT PART TO PRESERVER ORDER"
            DefaultSession += Chr(13) + "//WITHIN THE QUESTS. TARQU WILL BE AUTOMATED BY THE QUEST EDITOR, BUT MANUALLY EDITING"
            DefaultSession += Chr(13) + "//IT HERE IS DANGEROUS. "
            DefaultSession += Chr(13) + "////////////////////////////////////////////////////////////////////"
            DefaultSession += Chr(13) + "CAMPA#&(Blank Campaign)"
            DefaultSession += Chr(13) + "SESSI#&(Blank Session)"
            DefaultSession += Chr(13) + "INITM#&(0)"
            DefaultSession += Chr(13)
            DefaultSession += Chr(13) + "TOTAL#1(1)"
            DefaultSession += Chr(13)
            DefaultSession += Chr(13) + "SCRIP#1(1)"
            DefaultSession += Chr(13) + "OBJEC#1(1)"
            DefaultSession += Chr(13) + "ONCOM#1(0)"
            DefaultSession += Chr(13) + "INTRO#1(Blank Introduction text.)"
            DefaultSession += Chr(13) + "OPTIO#1(1.[Blank Option])"
            DefaultSession += Chr(13) + "REFRE#1(Blank Refresh Response text.)"
            DefaultSession += Chr(13) + "SUBO1#1(Blank Response 1.)"
            SaveTextToFile(DefaultSession, SessionDir)
            SessionList.Items.Add("Session " + LTrim(Str(SessionList.Items.Count + 1)))
            SessionList.SelectedIndex = SessionList.Items.Count - 1
        Else
            SND("/RError: Campaign was hidden, direct fault. Reload quest editor.")
        End If
    End Sub
#End Region
End Class